Converts image data in a managed buffer from one color conversion model directly to RGB\BGR using built-in algorithms, and sets the output as a RasterImage.
public static void ConvertDirectToImage(
ConversionColorFormat srcFormat,
ConversionColorFormat destFormat,
byte[] srcBuffer,
int srcBufferOffset,
RasterImage image,
int width,
int height,
int inAlign,
int outAlign
)
srcFormat
Format of the input data.
destFormat
Format of the output data.
srcBuffer
A pointer to the buffer containing the input data.
srcBufferOffset
Offset to the first byte of the srcBuffer data buffer.
image
A RasterImage object that will hold the converted data.
width
Width of pixels to be processed.
height
Height of pixels to be processed.
inAlign
Each scanline in the input buffer is a multiple of inAlign bytes.
outAlign
Each scanline in the output buffer is a multiple of outAlign bytes.
There is no need to call the Start and Stop methods to initialize or stop the conversion engine.
Converting from any color space to YCCK color space is currently not supported.
For more information about Alignment Parameters, refer to Alignment Parameters.
To convert to the Y41P type, the input buffer length must be a multiple of 8.
This example will convert BGR image to Lab color space, and then convert the Lab buffer to RasterImage, using the static method ConvertDirectToImage.
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ColorConversion;
using Leadtools.ImageProcessing;
public string outputFile = Path.Combine(LEAD_VARS.ImagesDir, "LabToBgr.bmp");
public void ConvertDirectToImageExample()
{
// Start up the ColorConversion.
RasterColorConverterEngine.Startup();
// Input file name
string inputFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp");
// Load the input image as BGR
using (RasterCodecs codecs = new RasterCodecs())
using (RasterImage bgrImage = codecs.Load(inputFileName, 24, CodecsLoadByteOrder.Bgr, 1, 1))
{
// Get the input image buffer array
byte[] bgrBuffer = new byte[bgrImage.BytesPerLine * bgrImage.Height];
bgrImage.Access();
bgrImage.GetRow(0, bgrBuffer, 0, bgrImage.BytesPerLine * bgrImage.Height);
bgrImage.Release();
// Create the destination CIELab buffer
byte[] labBuffer = new byte[bgrBuffer.Length];
// Initialize a new Converter object
using (RasterColorConverterEngine colorConverterEngine = new RasterColorConverterEngine())
{
// Conversion parameters
ConversionParameters convParams = new ConversionParameters();
convParams.Method = ConversionMethodFlags.ChangeLab;
// CIELab conversion parameters
ConversionLabParameters labParameters = ConversionLabParameters.Empty;
labParameters.AOffset = 128;
labParameters.ARange = 170;
labParameters.BOffset = 96;
labParameters.BRange = 200;
labParameters.LOffset = 0;
labParameters.LRange = 100;
labParameters.Mask = ConversionLabMaskFlags.AOffset |
ConversionLabMaskFlags.ARange |
ConversionLabMaskFlags.BOffset |
ConversionLabMaskFlags.BRange |
ConversionLabMaskFlags.LOffset |
ConversionLabMaskFlags.LRange;
convParams.LabParameters = labParameters;
// Convert BGR to CIELab buffer using the previous parameters
colorConverterEngine.Start(ConversionColorFormat.Bgr, ConversionColorFormat.Lab, null);
colorConverterEngine.SetParameters(convParams);
colorConverterEngine.Convert(bgrBuffer, // input buffer
0, // offset from the beginning of the source buffer
labBuffer, // output buffer
0, // offset from the beginning of the destination buffer
bgrImage.Width, // pixels width
bgrImage.Height, // pixels height
bgrImage.BytesPerLine - (bgrImage.Width * (bgrImage.BitsPerPixel / 8)), // Scanline alignment for input buffer
0); // Scanline alignment for output buffer
colorConverterEngine.Stop();
// To view the image after converison, convert the now CIELab buffer to a BGR image
RasterImage dstImage = new RasterImage(RasterMemoryFlags.Conventional, bgrImage.Width, bgrImage.Height, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, null, IntPtr.Zero, 0);
RasterColorConverterEngine.ConvertDirectToImage(
ConversionColorFormat.Lab,
ConversionColorFormat.Bgr,
labBuffer, // Source buffer
0, // Offset from the beginning of the source buffer
dstImage, // Destination Image
bgrImage.Width, // Pixels width
bgrImage.Height, // Pixels height
0, // Scanline alignment for input buffer
bgrImage.BytesPerLine - (bgrImage.Width * (bgrImage.BitsPerPixel / 8))); // Scanline alignment for output buffer
// Save the converted RGB Image
codecs.Save(dstImage, outputFile, RasterImageFormat.Bmp, 24);
}
}
// Shutdown the ColorConversion.
RasterColorConverterEngine.Shutdown();
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images";
}